package com.woniu.account.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.woniu.account.dto.venueAdmin.VenueAdminDto;
import com.woniu.account.form.venueAdminRole.updateVenueAdminRoleForm;
import com.woniu.account.model.VenueRole;
import com.woniu.account.service.VenueAdminRoleService;
import com.woniu.account.service.VenueRoleService;
import com.woniu.exception.venueAdmin.VenueAdminException;
import com.woniu.exception.venueAdmin.VenueAdminExceptionCode;
import com.woniu.model.Result;
import com.woniu.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月11日
 */


@RestController
@RequestMapping("/venueRole")
@Api(tags = "管理员，查询所有")
public class VenueRoleController {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private VenueAdminRoleService venueAdminRoleService;
    @Autowired
    private VenueRoleService venueRoleService;

    @PostMapping("/updateVenueAdminRole")
    @RequiredRole("管理员")
    public Result updateAdminRole(@RequestBody updateVenueAdminRoleForm form) throws Exception {
        String token = WebUtil.getToken();
        String s = redisUtil.get(token);
        VenueAdminDto venueAdminDto = JSONUtil.toBean(JSONUtil.toJsonStr(JwtUtil.getVenueAdmin(s)),VenueAdminDto.class);
        if (venueAdminDto.getVenueAdminState() != StateUtil.VENUE_ADMIN_STATE_NORMAL)
            throw new VenueAdminException(VenueAdminExceptionCode.ACCOUNT_ADMIN_ABNORMAL);
        venueAdminRoleService.updateAdminRole(form);
        return Result.getResult().setMsg("成功").setData(venueAdminDto);
    }

    @ApiOperation("查询所有场馆角色")
    @GetMapping("/load")
    public Result load() {
        List<VenueRole> list = venueRoleService.list();
        return Result.getResult().setData(list).setMsg("成功");
    }
}

